﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using System.IO;
using System.Reflection;
using Metamorph.Configuration;
using System.Collections.Generic;

namespace Metamorph.Core
{
	public class AssemblyUtils
	{
		private static AssemblyUtils auInstance;

		private Dictionary<string, Assembly> dAssemblies;

		private AssemblyUtils()
		{
			dAssemblies = new Dictionary<string, Assembly>();
		}

		public static AssemblyUtils Instance
		{
			get
			{
				if (auInstance == null)
				{
					auInstance = new AssemblyUtils();
				}

				return auInstance;
			}
		}

		public Assembly GetCodeAssembly()
		{
			return GetAssembly(MetamorphConfiguration.Instance.UserCodeAssembly);
		}

		public Assembly GetClientMonitoringAssembly()
		{
			return GetAssembly(MetamorphConfiguration.Instance.MonitoringProvider.AssemblyBaseName + ".Client");
		}

		public Assembly GetServerMonitoringAssembly()
		{
			return GetAssembly(MetamorphConfiguration.Instance.MonitoringProvider.AssemblyBaseName + ".Server");
		}

		public Assembly GetAssembly(string sAssemblyName)
		{
			Assembly aAssembly = null;

			if (dAssemblies.ContainsKey(sAssemblyName))
			{
				aAssembly = dAssemblies[sAssemblyName];
			}
			else
			{
				aAssembly = LoadAssembly(sAssemblyName);

				dAssemblies.Add(sAssemblyName, aAssembly);
			}

			return aAssembly;
		}

		public Assembly LoadAssembly(string sAssemblyName)
		{
			Assembly aLoadedAssembly = LoadAssembly(Assembly.GetEntryAssembly(), sAssemblyName);

			if (aLoadedAssembly == null)
			{
				string sLocation = Assembly.GetExecutingAssembly().Location;

				sLocation = sLocation.Substring(0, sLocation.LastIndexOf(Path.DirectorySeparatorChar)) + Path.DirectorySeparatorChar + sAssemblyName + ".dll";
				aLoadedAssembly = Assembly.LoadFile(sLocation);
			}

			return aLoadedAssembly;
		}

		public Assembly LoadAssembly(Assembly aRootAssembly, string sAssemblyName)
		{
			Assembly aLoadedAssembly = null;
			bool bAssemblyFound = false;

			if (aRootAssembly != null)
			{
				if (aRootAssembly.GetName().Name.Equals(sAssemblyName))
				{
					aLoadedAssembly = aRootAssembly;

					bAssemblyFound = true;
				}
				foreach (AssemblyName anReferencedAssembly in aRootAssembly.GetReferencedAssemblies())
				{
					if (anReferencedAssembly.Name.Equals(sAssemblyName))
					{
						aLoadedAssembly = Assembly.Load(anReferencedAssembly);

						bAssemblyFound = true;

						break;
					}
				}

				if (!bAssemblyFound)
				{
					foreach (AssemblyName anReferencedAssembly in aRootAssembly.GetReferencedAssemblies())
					{
						if (!anReferencedAssembly.Name.ToLower().StartsWith("system") &&
							!anReferencedAssembly.Name.ToLower().StartsWith("microsoft") &&
							!anReferencedAssembly.Name.ToLower().StartsWith("mySql") &&
							!anReferencedAssembly.Name.ToLower().StartsWith("log4net") &&
							!anReferencedAssembly.Name.ToLower().StartsWith("mscorlib"))
						{
							aLoadedAssembly = LoadAssembly(Assembly.Load(anReferencedAssembly), sAssemblyName);

							if (aLoadedAssembly != null)
							{
								break;
							}
						}
					}
				}
			}

			return aLoadedAssembly;
		}
	}
}
